package com.ambulance.project.biz.controller.device;

import com.ambulance.common.exception.CustomException;
import com.ambulance.framework.web.domain.AjaxResult;
import com.ambulance.project.biz.dto.device.BrDataDto;
import com.ambulance.project.biz.dto.web.BreathInfoDto;
import com.ambulance.project.biz.entity.*;
import com.ambulance.project.biz.service.*;
import com.ambulance.utils.CommonUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author RXL
 * @Date 2020/4/29 10:44
 * @Version 1.0
 */
@Controller
@Api(tags = "可视化-设备信息")
@RequestMapping(value = "/dev/v1/device", method = RequestMethod.POST)
public class DeviceController {


    @Autowired
    private MyCustomerService myCustomerService;
    @Autowired
    private EcgDataInfoService ecgDataInfoService;
    @Autowired
    private EcgWaveInfoService ecgWaveInfoService;
    @Autowired
    private BrDataInfoService brDataInfoService;

    @Autowired
    private BrWaveInfoService brWaveInfoService;


    @ApiOperation(value = "ecg心电", notes = "ecg心电", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carID", value = "车辆标识", dataType = "string"),

    })
    @ApiResponses({
            @ApiResponse(code = 200, response = EcgDto.class, message = "响应")
    })
    @RequestMapping(value = "/ecg/info")
    @ResponseBody
    public Object ecgInfo(
            @RequestParam(value = "carID", defaultValue = "7") Integer carID
    ) {
        // 模拟接口
        QueryWrapper<EcgDataInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("carID", carID);
        Dispatch dispatch = myCustomerService.getRecentDispatchByCarID(carID);

        wrapper.eq("dispatchID", dispatch.getDispatchID());
        EcgDataInfo one = ecgDataInfoService.getOne(wrapper);
        if (CommonUtils.checkNull(one)) {
            throw new CustomException("数据不存在");
        }
        QueryWrapper<EcgWaveInfo> waveData = new QueryWrapper<>();
        waveData.eq("refer", one.getId());
        List<EcgWaveInfo> ecgWaveInfos = ecgWaveInfoService.list(waveData);


        EcgDto ecgDto = new EcgDto();
        ecgDto.setDevice("baoRiDevice");
        ecgDto.setEcg_heart_rate(one.getEcg());
        ecgDto.setNibp_diastolic(one.getNibp());
        ecgDto.setSpo2_pulse_rate(one.getSpo2());
        ecgDto.setTthor_resp_rate(one.getResp());

        ecgDto.setMdc_ecg_elec_potl_i(ecgWaveInfos.get(0).getData());
        ecgDto.setMdc_ecg_elec_potl_ii(ecgWaveInfos.get(0).getData());
        ecgDto.setMdc_ecg_elec_potl_iii(ecgWaveInfos.get(0).getData());
        ecgDto.setMdc_ecg_elec_potl_avr(ecgWaveInfos.get(0).getData());
        ecgDto.setMdc_ecg_elec_potl_avl(ecgWaveInfos.get(0).getData());
        ecgDto.setMdc_ecg_elec_potl_avl(ecgWaveInfos.get(0).getData());
        ecgDto.setMdc_ecg_elec_potl_v1(ecgWaveInfos.get(0).getData());
        ecgDto.setMdc_ecg_elec_potl_v2(ecgWaveInfos.get(0).getData());
        ecgDto.setMdc_ecg_elec_potl_v3(ecgWaveInfos.get(0).getData());
        ecgDto.setMdc_ecg_elec_potl_v4(ecgWaveInfos.get(0).getData());
        ecgDto.setMdc_ecg_elec_potl_v5(ecgWaveInfos.get(0).getData());
        ecgDto.setMdc_ecg_elec_potl_v6(ecgWaveInfos.get(0).getData());


//        ecgDto.setSpo2_pulse_rate(one.getS);
//        EcgDataDto ecgDataDto = new EcgDataDto();
//        ecgDataDto.setEcgDataInfo(one);
//        ecgDataDto.setEcgWaveInfo(ecgWaveInfos);

        return AjaxResult.success(ecgDto);

    }

    @ApiOperation(value = "br呼吸机", notes = "br呼吸机", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carID", value = "车辆标识", dataType = "string"),

    })
    @ApiResponses({
            @ApiResponse(code = 200, response = BreathInfoDto.class, message = "响应")
    })
    @RequestMapping(value = "/br/info")
    @ResponseBody
    public Object brInfo(
            @RequestParam(value = "carID", defaultValue = "7") Integer carID
    ) {

        QueryWrapper<BrDataInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("carID", carID);
        Dispatch dispatch = myCustomerService.getRecentDispatchByCarID(carID);
        wrapper.eq("dispatchID", dispatch.getDispatchID());
        BrDataInfo one = brDataInfoService.getOne(wrapper);
        if (CommonUtils.checkNull(one)) {
            throw new CustomException("数据不存在");
        }

        QueryWrapper<BrWaveInfo> waveData = new QueryWrapper<>();
        waveData.eq("refer", one.getId());

        List<BrWaveInfo> BrWaveInfo = brWaveInfoService.list(waveData);

        BrDataDto brDataDto = new BrDataDto();
        brDataDto.setEcgDataInfo(one);
        brDataDto.setEcgWaveInfo(BrWaveInfo);
        final BreathInfoDto breathInfoDto = new BreathInfoDto();
        breathInfoDto.setCarID(carID.toString());
        breathInfoDto.setDispatchID(dispatch.getDispatchID().toString());
        breathInfoDto.setVti(one.getVti());
        breathInfoDto.setFsp(one.getFsp());
        breathInfoDto.setMvi(one.getMvi());
        breathInfoDto.setEtCO2(one.getEtco2());
        breathInfoDto.setFiO2(one.getFio2());
        breathInfoDto.setPpeak(one.getPpeak());
        breathInfoDto.setPmean(one.getPmean());
        breathInfoDto.setVt(one.getVt());
        breathInfoDto.setIE("1:1.7");
        breathInfoDto.setFreq(one.getFreq());
        breathInfoDto.setPmax(one.getPmax());
        breathInfoDto.setCoWave(one.getWave());
        breathInfoDto.setAlarmInfo("2.7");
        //        breathInfoDto.setIE
        return AjaxResult.success(breathInfoDto);

    }


}





















